package org.jeecg.modules.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yaorange.jxc.goods.entity.Goods;
import com.yaorange.jxc.goods.entity.GoodsType;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.api.vo.PageVo;
import org.jeecg.modules.mapper.GoodsMapper;
import org.jeecg.modules.mapper.GoodsTypeMapper;
import org.jeecg.modules.service.GoodsTypeService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @company: yaorange
 * @author: Andy.Li
 * @Date: 2022.04.28 11:25:37
 */
@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {
    @Resource
    private GoodsTypeMapper goodsTypeMapper;
    @Resource
    private GoodsMapper goodsMapper;

    @Override
    @Transactional(readOnly = true)
    public List<GoodsType> selectGoodsType(String body) {
        //判断是否为空
        if (body == null) {
            return null;
        }
        //不为空，解析json
        JSONObject object = JSONObject.parseObject(body);

        String id = object.getString("pId");
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        Integer pId = Integer.parseInt(id);
        //如果为根节点
        return goodsTypeMapper.selectGoodsType(pId);
    }

    @Override
    @Transactional(readOnly = true)
    public PageVo<Goods> getAllGoodsList(Long current, Long pageSize, String goodsName,
                                         Integer goodsTypeId) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(goodsName)) {
            wrapper.like("goods_name", goodsName);
        }
        if (goodsTypeId != null) {
            wrapper.eq("goods_type_id", goodsTypeId);
        }
        Page<Goods> page = goodsMapper.selectPage(new Page<Goods>(current, pageSize), wrapper);
        PageVo<Goods> vo = new PageVo<>();
        vo.setTotal(page.getTotal());
        vo.setPages(page.getPages());
        vo.setList(page.getRecords());
        return vo;
    }

    @Override
    @Transactional(readOnly = true)
    public Goods selectGoodsById(Integer goodsId) {
        return goodsMapper.selectById(goodsId);
    }
}
